package tvv.levelup.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import tvv.levelup.models.Comment;
import tvv.levelup.models.News;
import tvv.levelup.service.CommentService;
import tvv.levelup.service.ICommentService;
import tvv.levelup.service.INewsService;
import tvv.levelup.service.NewsService;

import java.util.List;

/**
 * Created by VLAD on 09.09.2014.
 */
@Controller
public class NewsController {

    @Autowired
    private ICommentService commentService;
    @Autowired
    private INewsService newsService;

    public Comment saveComment(String message, long authorId){
        return commentService.saveComment(message, authorId);
    }

    public void deleteComment(Comment comment){
        commentService.deleteComment(comment);
    }

    public News saveNews(String text) {
        return newsService.saveNews(text);
    }

    public void deleteNews(News news){
        newsService.deleteNews(news);
    }

    public News addCommentToNews(long newsId, long comId){

        News news = newsService.findNewsById(newsId);
        Comment comment = commentService.findCommentById(comId);
        news.getComments().add(comment);
        return newsService.updateNews(news);

    }

    public News getNewsById(int id) {
        return newsService.findNewsById(id);
    }

    public List<News> getAllNews(){
        return newsService.findAllNews();
    }


}
